<template>
	<div class="daojishi">
		<p>距离<span style="color:red;">{{datename}}</span>还有</p>
		{{day}}天{{h}}时{{m}}分{{s}}
	</div>
</template>
<script>
	export default{
		props:{
			datename:{
				type:String,
				value:'月底'
			},
			date:{
				type:String,
				value:''
			}
		},
		data(){
			return{
				day:0,
				h:0,
				m:0,
				s:0,
				t:null
			}
		},
		created(){
			this.t = setInterval(()=>{
				this.begin();
			},1000);
		},
		methods:{
			  // 获取当月天数
			  mGetDate(year, month,day) {
			    var d = new Date(year, month, day);
			    return d.getDate();
			  },
			  //获取当月最后一天的时间戳
			  mGetTimes(h,m,s){
			  	return h*60*60+m*60+s;
			  },
			  // 倒计时
			  begin(){
	  				let curD = new Date();  //获取当前日期
					let curDay = curD.getDate(),curMonth = curD.getMonth()+1,year = curD.getFullYear();  //获取当前年月日
					let days = this.date == ""?this.mGetDate(curD.getFullYear(),curD.getMonth()+1,0):this.mGetDate(this.splitDate(this.date)[0],this.splitDate(this.date)[1],this.splitDate(this.date)[2]);  //获取本月最后一天
					this.day = this.addZore(days - curDay);  //计算剩余天数
					let times = this.mGetTimes(23,59,59),curTimes = this.mGetTimes(curD.getHours(),curD.getMinutes(),curD.getSeconds());  //获取当前时间和23:59:59的时间戳对比
					if(times - curTimes ===0){clearInterval(this.t);}
					let h = (times - curTimes)/60/60;  //获取剩余小时
					this.h = this.addZore(Math.floor(h));  //去除小数点后面的数字，保留整数
					let m = (h-this.h)*60;
					this.m = this.addZore(Math.floor(m));
					let s = (m-this.m)*60;
					this.s = this.addZore(Math.round(s));
			  },
			  // 分割日期
			  splitDate(e){
			  	return e.split('-');
			  },
			  // 添加0
			  addZore(e){return e>0?e:'0'+e}
		}
	}
</script>